/**
 * 
 */
package org.compiere.model;

import java.util.Properties;

import org.compiere.util.DB;

/**
 * @author Aslanbek
 *
 */
public class Is5BOpForm extends CalloutEngine {

	public String createSQLforCell(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
	{
		String result = null;
		MBPMForm form = null;
		MBPMFormLine formLine = null;
		form = new MBPMForm(ctx, (int)mTab.getValue(I_BPM_Form.COLUMNNAME_BPM_Form_ID), null);
		formLine = new MBPMFormLine(ctx, (int)mTab.getValue(I_BPM_FormLine.COLUMNNAME_BPM_FormLine_ID), null);
		
		if (value != null && form!=null && formLine!=null) 
		{
			boolean is5BOpForm = form.getName().contains("5БО(п)");
//			if(is5BOpForm)
//			{
				System.out.println("Is5BOpForm");
				
				MCharge charge = new MCharge(ctx, (int)value, null);
				MCharge[] childCharges = null;
				if(charge.getFI_Code().equals("2.3.1")||charge.getFI_Code().equals("2.3.2"))
					childCharges = charge.getChildCharges(charge.getFI_Code()+".");
				else
					childCharges = charge.getChildCharges(charge.getFI_Code());
				
				int i=0;
				for(MBPMFormCell cell :MBPMFormLine.getLines(ctx, formLine.getBPM_FormLine_ID(), null))
				{
					if(cell.getBPM_FormColumn().getC_Period_ID()>0 && cell.get_Value("BPM_Form_Meaning").equals("План")) 
					{
						//Create Calculation
						int BPM_Project_ID = DB.getSQLValue(null
								, "SELECT BPM_Project_ID FROM BPM_Project WHERE BPM_VersionBudget_ID = "
										+ form.getBPM_VersionBudget_ID()); 
						
						//Set new calculation for parameter line
						MParameter parameter = new MParameter(ctx, cell.getBSC_Parameter_ID(), null);
						MParameterLine parameterLine = parameter.getZeroParameterLine();
						parameterLine.setIsImported(true);
						MPFRCalculation calculation = null;
						if(parameterLine.getPFR_Calculation()!=null)
						{
							calculation = new MPFRCalculation(ctx, parameterLine.getPFR_Calculation_ID(), null);
							calculation.delete(true);
						}
						
						calculation = new MPFRCalculation(ctx, 0, null);
						calculation.setName(form.getName()+"_LineNo_"+formLine.getLineNo()+"_Col_"+cell.getBPM_FormColumn().getC_Period().getName()+"_autoNo_"+i);
						calculation.setAD_Table_ID(1000331);
						calculation.setAD_Column_ID(1005422);
						calculation.setPFR_CalcType_ID(1000000);
						calculation.saveEx();
						
						parameterLine.setPFR_Calculation_ID(calculation.getPFR_Calculation_ID());
						parameterLine.saveEx();
						 //--first condition
						MPFRWhereClause whereClause1 = new MPFRWhereClause(ctx, 0, null);
						whereClause1.setPFR_Calculation_ID(calculation.getPFR_Calculation_ID());
						whereClause1.setAD_Column_ID(1005429);
						whereClause1.setAndOr(null);
						whereClause1.setOperation("=");
						whereClause1.setValue1(String.valueOf(BPM_Project_ID>0?BPM_Project_ID:1000030));
						whereClause1.setColumnName(MBPMProject.COLUMNNAME_BPM_Project_ID);
						whereClause1.setLine(10);
						whereClause1.setisStatic(true);
						whereClause1.saveEx();
						
						//--second condition
						MPFRWhereClause whereClause2 = new MPFRWhereClause(ctx, 0, null);
						whereClause2.setPFR_Calculation_ID(calculation.getPFR_Calculation_ID());
						whereClause2.setAD_Column_ID(1005421);
						whereClause2.setAndOr("AND");
						whereClause2.setOperation("=");
						whereClause2.setValue1(String.valueOf(cell.getBPM_FormColumn().getC_Period_ID()));
						whereClause2.setColumnName(MPeriod.COLUMNNAME_C_Period_ID);
						whereClause2.setLine(20);
						whereClause2.setisStatic(true);
						whereClause2.saveEx();
						
						//--third condition
						for(int j=0; j<childCharges.length; j++)
						{
							MPFRWhereClause whereClause3 = new MPFRWhereClause(ctx, 0, null);
							whereClause3.setPFR_Calculation_ID(calculation.getPFR_Calculation_ID());
							
							if(j==0)
							{
								whereClause3.setAndOr("AND");
								whereClause3.setOpenBracket("(");
							}
							
							if(j>0)
								whereClause3.setAndOr("OR");
							 
							if(j==(childCharges.length-1))
								whereClause3.setCloseBracket(")");
							
							whereClause3.setAD_Column_ID(1005426);							
							whereClause3.setOperation("=");
							whereClause3.setValue1(String.valueOf(childCharges[j].getC_Charge_ID()));//C_Charge_ID
							whereClause3.setColumnName(MCharge.COLUMNNAME_C_Charge_ID);
							whereClause3.setLine(30+j);
							whereClause3.setisStatic(true);
							whereClause3.saveEx();
						}						
						i++;
					}
					else
					{
						continue;
					}
				}
//			}
//			else
//			{
//				result = null;
//			}
		}
		else
		{
			if(form!=null && formLine!=null)
			{
				for(MBPMFormCell cell :MBPMFormLine.getLines(ctx, formLine.getBPM_FormLine_ID(), null))
				{
					if(cell.getBPM_FormColumn().getC_Period_ID()>0 && cell.get_Value("BPM_Form_Meaning").equals("План")) 
					{
						MParameter parameter = new MParameter(ctx, cell.getBSC_Parameter_ID(), null);
						MParameterLine parameterLine = parameter.getZeroParameterLine();
						MPFRCalculation calculation = null;
						if(parameterLine.getPFR_Calculation()!=null)
						{
							calculation = new MPFRCalculation(ctx, parameterLine.getPFR_Calculation_ID(), null);
							calculation.delete(true);
						}
						parameterLine.setPFR_Calculation_ID(0);
						parameterLine.setIsImported(false);
						parameterLine.saveEx();
					}
				}
			}
		}
		return result;
	}
}
